<?php
require_once 'fcCommon.php';

class Entity extends Map{
    private $tableName;
    public function __construct($tableName) {
        $this->tableName = $tableName;
    }
    
    public function tableName(){
        return $this->tableName;
    }
}

class Connection{
    private $host = "localhost"; 
    private $user = "root"; 
    private $pass = "1234"; 
    private $db = "test";
    
    public function connect() {
        $conn = mysql_connect($this->host, $this->user, $this->pass) or die("Error : ไม่สามารถเชื่อมต่อฐานข้อมูลได้");
        mysql_select_db($this->db, $conn) or die("Error : ไม่สามารถเลือกฐานข้อมูลได้");
        return $conn;
    }
    
    private function preCommand($conn){
        mysql_query("set character_set_results='utf8'", $conn);
        mysql_query("set character_set_client='utf8'", $conn);
        mysql_query("set character_set_connection='utf8'", $conn);
    }
    
    public function persist(Entity $obj){
        $sqlStr = "INSERT INTO ";
        $sqlStr .= $obj->tableName()."(";
        $keys = $obj->keys();
        $i = 0;
        foreach($keys as $key){
            if($i > 0){
                $sqlStr .= ",";
            }
            $sqlStr .= $key;
            $i++;
        }
        $sqlStr .= ") VALUES(";
        $i = 0;
        foreach($keys as $key){
            if($i > 0){
                $sqlStr .= ",";
            }
            $val = $obj->get($key);
            if(is_string($val)){
                $sqlStr .= "'".$val."'";
            }else{
                $sqlStr .= $val;
            }
            $i++;
        }
        $sqlStr .= ");";
        //echo $sqlStr;
        
        $conn = $this->connect();
        $this->preCommand($conn);
        mysql_query($sqlStr, $conn);
        mysql_close($conn);
    }
    
    public function find(Entity $obj){
        $sqlStr = "SELECT * FROM ";
        $sqlStr .= $obj->tableName();
        
        $keys = $obj->keys();
        if(sizeof($keys) == 0){
        }else{
            $sqlStr .= " WHERE ";
        }   
        
        $i = 0;
        foreach($keys as $key){
            if($i > 0){
                $sqlStr .= " AND ";
            }
            $val = $obj->get($key);
            if(is_string($val)){
                $sqlStr .= $key."='".$val."'";
            }else{
                $sqlStr .= $key."=".$val;
            }
            $i++;
        }
        //echo $sqlStr."<br/>";
        
        $conn = $this->connect();
        $this->preCommand($conn);
        $result = mysql_query($sqlStr, $conn) or die("Error : เกิดข้อผิดพลาด ไม่เจอข้อมูลที่ต้องการ");
        $i = 0;
        $res = array();
        while($row = mysql_fetch_array($result)){
            $resObj = new Entity($obj->tableName());
            $keys = array_keys($row);
            foreach($keys as $key){
                if(!is_string($key)){
                    continue;
                }
                $resObj->put($key, $row[$key]);
            }
            $res[$i] = $resObj;
            $i++;
        }
        mysql_close($conn);
        
        return $res;
    }
    
    public function merge(Entity $obj, Map $condition){
        $sqlStr = "UPDATE ".$obj->tableName()." SET ";
        $keys = $obj->keys();
        $i = 0;
        foreach($keys as $key){
            if($i > 0){
                $sqlStr .= ",";
            }
            $val = $obj->get($key);
            if(is_string($val)){
                $sqlStr .= $key."='".$val."'";
            }else{
                $sqlStr .= $key."=".$val;
            }
            $i++;
        }
        $sqlStr .= " WHERE ";
        $keys = $condition->keys();
        $i = 0;
        foreach($keys as $key){
            if($i > 0){
                $sqlStr .= " AND ";
            }
            $val = $condition->get($key);
            if(is_string($val)){
                $sqlStr .= $key."='".$val."'";
            }else{
                $sqlStr .= $key."=".$val;
            }
            $i++;
        }
        
        //echo $sqlStr;
        
        $conn = $this->connect();
        $this->preCommand($conn);
        mysql_query($sqlStr, $conn);
        mysql_close($conn);
    }
    
    public function remove(Entity $obj){
        $sqlStr = "DELETE FROM ".$obj->tableName();
        $sqlStr .= " WHERE ";
        $keys = $obj->keys();
        $i = 0;
        foreach($keys as $key){
            if($i > 0){
                $sqlStr .= " AND ";
            }
            $val = $obj->get($key);
            if(is_string($val)){
                $sqlStr .= $key."='".$val."'";
            }else{
                $sqlStr .= $key."=".$val;
            }
            $i++;
        }
        //echo $sqlStr;
        
        $conn = $this->connect();
        $this->preCommand($conn);
        mysql_query($sqlStr, $conn);
        mysql_close($conn);
    }
}
?>
